greedy implementation math sortings *900

Please click on ads to support us..

Python Code:



n=int(input())
a=sorted([int(x) for x in input().split()])
somme=0
for x in range(n//2):
    somme+=(a[x]+a[-x-1])**2
print(somme)


C++ Code:

#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#define ll long long
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define joan ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
ll n,a[300000+3];
int main()
{
    joan;
cin>>n;
for(int i=0;i<n;i++)
    cin>>a[i];
sort(a,a+n);
ll ans=0;
for(int i=0;i<n/2;i++)
{
ans+=pow(a[i]+a[n-1-i],2);
}
cout<<ans;
}


Comments

Submit
0 Comments
More Questions

1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe